package com.hzqy.service.system;


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;


@Service
public class SysVoProcessServiceBeen{
	protected final Logger LOG = LoggerFactory.getLogger(getClass());

	public static final String resolutionFieldName = "resolutionName";


	/*
	 *
	 * 如果有 f_开头 _resolution结尾的属性，将属性值注入 resolutionName属性内
	 *
	 *
	 */
	public <T> void voProcess(List<T> list) {
		if(list == null || list.size() <= 0)return;
		Class<?> cls = list.get(0).getClass();
		Field [] fields = cls.getDeclaredFields();
		for(Field f : fields){
			try {
				if(f.getName().startsWith("f_") && f.getName().endsWith("_resolution")){
					processResolution(f, cls, list);
				}
			} catch (Exception e) {
				LOG.error("处理属性" + f.getName() + "失败:", e);
			}
		}
	}



	protected final <T> void processResolution(Field f, Class<?> cls, List<T> list){
		try {
			boolean fieldAccessible = f.isAccessible();
			//f.setAccessible(true);
			ReflectionUtils.makeAccessible(f);
			Field resolutionField = cls.getDeclaredField(resolutionFieldName);
			boolean resolutionAccessible = resolutionField.isAccessible();
			//resolutionField.setAccessible(true);
			ReflectionUtils.makeAccessible(resolutionField);
			for(T t : list){
				int resolution = f.getInt(t);
				String resolutionStr = "未知";
				if(resolution == 1) resolutionStr = "标清";
				if(resolution == 2) resolutionStr = "高清";
				resolutionField.set(t, resolutionStr);
			}
			/*resolutionField.setAccessible(resolutionAccessible);
			f.setAccessible(fieldAccessible);*/
			/*if(!resolutionAccessible)
				resolutionField.setAccessible(false);
			if(!fieldAccessible){
				f.setAccessible(false);
			}*/
		} catch (Exception e) {
			LOG.error("处理resolution失败:", e);
		}

	}



}